package com.woniuxy.three.pms.order.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.woniuxy.three.pms.bean.GuestBean;
import com.woniuxy.three.pms.bean.OrderBean;
import com.woniuxy.three.pms.bean.OrderChildBean;
import com.woniuxy.three.pms.bean.RoomTypeBean;
import com.woniuxy.three.pms.bean.UserBean;
import com.woniuxy.three.pms.config.AlipayConfig;
import com.woniuxy.three.pms.dao.GuestBeanMapper;
import com.woniuxy.three.pms.dao.OrderBeanMapper;
import com.woniuxy.three.pms.dao.OrderChildBeanMapper;
import com.woniuxy.three.pms.dao.RoomTypeBeanMapper;
import com.woniuxy.three.pms.order.service.IOrderService;
import com.woniuxy.three.pms.util.GetTime;

/**
 * 
*@ProjectName:Pms
*@Date:2019年6月20日
*@ClassName:OrderService.java
*@Author:ThinkPad
*@Description:TODO orderservice类
*@Version:版本
*@See：参考版本
*
 */
@Service
public class OrderService implements IOrderService {
		@Autowired
		private OrderBeanMapper  orderdao;
		@Autowired
		private OrderChildBeanMapper orderChilddao;
		@Autowired
		private RoomTypeBeanMapper  roomTypedao;
		@Autowired
		private GuestBeanMapper   guestdao;
		
		
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
	@Override
	public int deleteByPrimaryKey(Integer id) {
		
		orderChilddao.deleteByOrderid(id);
		int number=orderdao.deleteByPrimaryKey(id);
		return number;
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
	@Override
	public String insert(OrderBean record)  {
				
		List<OrderChildBean> listOrderChild=record.getOcb();
				//新建订单得到带有订单自增的订单对象
				orderdao.insertSelective(record);
				
				//循环插入子订单
				for (OrderChildBean orderChildBean : listOrderChild) {
					//将带有自增id的订单对象给子订单
					orderChildBean.setOrderId(record);
					//生成子订单同时生成宾客表，并得到自增id
					guestdao.insert(orderChildBean.getGuestId());
					//插入子订单
					orderChilddao.insert(orderChildBean);
				}
			return "生成订单成功";
	}
	
	@Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED)
	@Override
	public String insertSelective(OrderBean record) throws RuntimeException {
		String str="生成订单失败";
		//新建订单得到带有订单自增的订单对象
		orderdao.insertSelective(record);
		//得到子订单集合
		List<OrderChildBean> listOrderChild=record.getOcb();
		int number=0;
		//循环插入子订单
		for (OrderChildBean orderChildBean : listOrderChild) {
			//将带有自增id的订单对象给子订单
			orderChildBean.setOrderId(record);
			
			//更改库存			
			
			//得到开始时间与当前时间的差值 Long
			Long start_now=GetTime.gettime(orderChildBean.getStartTime());
			 //得到结束时间与当前时间的差值 Long
			Long end_now=GetTime.gettime(orderChildBean.getEndTime());
			
			//通过预订的房间种类
			 RoomTypeBean roomTypedata=roomTypedao.selectByPrimaryKey(orderChildBean.getRoomtype().getId()); 					
			 //得到开始时间对应库存的key
			 int key=(int)(start_now+1); 
			 int endkey=(int)(end_now+1);
			 /**
			  * 遍历减去预定开始时间到结束时间的存存，并判断存库是否充足，
			  * 不足则手动跑出运行异常，试主订单事务回滚
			  */
			 System.out.println("key:"+key);
			 for (int i = key; i < endkey; i++) {
				 switch (i) {														
				case 1:
						
							Integer count1=roomTypedata.getStock1()-1;
							if(count1<0) {
								
								throw new RuntimeException("库存不足");			
											
							}else {
								roomTypedata.setStock1(roomTypedata.getStock1()-1);
								continue;
							}
					
																			
				case 2:
					
						Integer count2=roomTypedata.getStock2()-1;
						System.out.println("库存测试"+count2);
						if(count2<0) {
							System.out.println("测试库存");
							throw new RuntimeException("库存不足");
						
						}else {
							roomTypedata.setStock2(roomTypedata.getStock2()-1);
							continue;
						}
					
					
				case 3:
					
					
						Integer count3=roomTypedata.getStock3()-1;
						if(count3<0) {
							
							throw new RuntimeException("库存不足");
							
						}else {
							roomTypedata.setStock3(roomTypedata.getStock3()-1);				
							continue;
						}
								
									
								
				case 4:
					
					

						Integer count4=roomTypedata.getStock4()-1;
						if(count4<0) {
							throw new RuntimeException("库存不足");
						}else {
							roomTypedata.setStock4(roomTypedata.getStock4()-1);
							continue;
						}
								
				
					
					
				case 5:
					
					
						Integer count5=roomTypedata.getStock5()-1;
						if(count5<0) {
							throw new RuntimeException("库存不足");
							
						}else {
							roomTypedata.setStock5(roomTypedata.getStock5()-1);
							continue;
						}
				
					
					
				case 6:
					
					

						Integer count6=roomTypedata.getStock6()-1;
						if(count6<0) {
							throw new RuntimeException("库存不足");
							
						}else {
							roomTypedata.setStock6(roomTypedata.getStock6()-1);
							continue;
						}
						
				case 7:
					
						Integer count7=roomTypedata.getStock7()-1;
						if(count7<0) {
							throw new RuntimeException("库存不足");
							
						}else {
							roomTypedata.setStock7(roomTypedata.getStock7()-1);
							continue;
						}
									
				default:
				
					str="选择日期错误";
					break;
				
				}
			 }
			
			
			roomTypedao.updateByPrimaryKeySelective(roomTypedata);
			//生成子订单同时生成宾客表，并得到自增id
		
			guestdao.insert(orderChildBean.getGuestId());
			//插入子订单
			number=orderChilddao.insert(orderChildBean);
		}
		if(number>0) {
			str="生成订单成功";
		}
		
		return str;
		
	}

	@Override
	public int updateByPrimaryKeySelective(OrderBean record) {
		int number=orderdao.updateByPrimaryKeySelective(record);
		return number;
	}

	@Override
	public int updateByPrimaryKey(OrderBean record) {
		int number=orderdao.updateByPrimaryKey(record);
		return number;
	}

	@Override
	public OrderBean findbyid(Integer id) {
	
		OrderBean bean=orderdao.findbyid(id);
		return bean;
	}

	@Override
	public List<OrderBean> findbyuserid(Integer userid) {
		List<OrderBean> list=orderdao.findbyuserid(userid);
		return list;
	}

	@Override
	public List<OrderBean> findbyuserattr(UserBean user) {
		List<OrderBean> list=orderdao.findbyuserattr(user);
		return list;
	}

	@Override
	public List<OrderBean> findall() {
		List<OrderBean> list=orderdao.findall();
		return list;
	}

	@Override
	public List<OrderBean> findbystate(String state) {
		List<OrderBean> list=orderdao.findbystate(state);
		return list;
	}
	/**
	 * 进行取消订单增加库存的操作
	 */
	@Override
	public void cancelOrderByid(Integer id) {
		
		OrderBean orderdata=orderdao.findbyid(id);
		
				//得到子订单集合
				List<OrderChildBean> listOrderChild=orderdata.getOcb();
				
				//遍历子订单
				for (OrderChildBean orderChildBean : listOrderChild) {
					
					//更改库存			
					
					//得到开始时间与当前时间的差值 Long
					Long start_now=GetTime.gettime(orderChildBean.getStartTime());
					 //得到结束时间与当前时间的差值 Long
					Long end_now=GetTime.gettime(orderChildBean.getEndTime());
					
					//通过预订的房间种类
					 RoomTypeBean roomTypedata=roomTypedao.selectByPrimaryKey(orderChildBean.getRoomtype().getId()); 					
					 //得到开始时间对应库存的key
					 int key=(int)(start_now+1); 
					 int endkey=(int)(end_now+1);
															
					 for (int i = key; i < endkey; i++) {
						 switch (i) {
						case 1:
							roomTypedata.setStock1(roomTypedata.getStock1()+1);
							continue;
							
						case 2:
						
							roomTypedata.setStock2(roomTypedata.getStock2()+1);
							continue;
						case 3:
							roomTypedata.setStock3(roomTypedata.getStock3()+1);				
							continue;
						case 4:
							roomTypedata.setStock4(roomTypedata.getStock4()+1);
							continue;
						case 5:
							roomTypedata.setStock5(roomTypedata.getStock5()+1);
							continue;
						case 6:
							roomTypedata.setStock6(roomTypedata.getStock6()+1);
							continue;
						case 7:
							roomTypedata.setStock7(roomTypedata.getStock7()+1);
							continue;
						
						}
					 }
					
					 roomTypedao.updateByPrimaryKeySelective(roomTypedata);
			}
				
}

	@Override
	public OrderBean findbyOrderid(String orderId) {
		OrderBean bean=orderdao.findbyOrderid(orderId);
		return bean;
	}
	
	@Override
	public OrderBean findbyOrderid1(String orderId) {
		OrderBean bean=orderdao.findByOrderNumber(orderId);
		return bean;
	}

	@Override
	public List<OrderBean> findbyGuestattr(GuestBean guest) {

		List<OrderBean> list=orderdao.findbyGuestattr(guest);
		
		return list;
	}

	@Override
	public List<OrderBean> findbyPaystate(String paystate) {
		List<OrderBean> list=orderdao.findbyPaystate(paystate);
		return list;
	}
	
	@Override
	public int updateByorderId(OrderChildBean record) {
		int number=orderChilddao.updateByorderId(record);
		return number;
	}

	@Override
	public List<OrderBean> findGuestByGuestName(GuestBean record) {
		List<OrderBean> list=orderdao.findGuestByGuestName(record);
		return list;
	}


}